package com.xpq.petserver.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xpq.petserver.converter.BailmentDataConverter;
import com.xpq.petserver.entity.R;
import com.xpq.petserver.entity.dto.PublicFosterageDTO;
import com.xpq.petserver.entity.dto.SearchBailmentDataDTO;
import com.xpq.petserver.entity.request.bailmentData.GetMyFosterageRequest;
import com.xpq.petserver.entity.request.bailmentData.PublicFosterageRequest;
import com.xpq.petserver.entity.request.bailmentData.SearchBailmentDataRequest;
import com.xpq.petserver.service.BailmentDataService;
import com.xpq.petserver.util.JwtUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 寄养数据相关请求接口
 *
 * @author XPQ
 * @date 2022-05-03
 */
@Slf4j
@Tag(name = "寄养数据模块")
@CrossOrigin
@RestController
@RequestMapping("/bailmentData")
public class BailmentDataController {
    @Autowired
    private BailmentDataService bailmentDataService;

    /**
     * 发布寄养消息接口
     * @param multipartFiles
     * @param data
     * @return
     */
    @Operation(summary = "发布寄养消息接口")
    @Parameters({
            @Parameter(name = "files", description = "寄养图片"),
            @Parameter(name = "data", description = "寄养数据")
    })
    @PostMapping("/publicFosterage")
    public R publicFosterage(@RequestPart("files") List<MultipartFile> multipartFiles,
                             @RequestPart("data") String data,
                             HttpServletRequest request) throws JsonProcessingException {
        ObjectMapper mapperBuilder = new ObjectMapper();
        PublicFosterageRequest publicFosterageRequest = mapperBuilder.readValue(data, PublicFosterageRequest.class);
        PublicFosterageDTO publicFosterageDTO = BailmentDataConverter.INSTANCE.toPublicFosterageDTO(publicFosterageRequest);
        String token = request.getHeader("token");
        publicFosterageDTO.setUserId(JwtUtil.getId(token));
        return bailmentDataService.publicFosterage(multipartFiles, publicFosterageDTO);
    }

    /**
     * 获取用户寄养数据接口
     * @param request
     * @return
     */
    @Operation(summary = "获取用户寄养数据接口")
    @Parameter(name = "getMyFosterageRequest", description = "获取用户寄养数据请求参数")
    @PostMapping("/getMyFosterage")
    public R getMyFosterage(@RequestBody GetMyFosterageRequest getMyFosterageRequest, HttpServletRequest request) {
        String token = request.getHeader("token");
        return bailmentDataService.getMyFosterage(
                JwtUtil.getId(token),
                getMyFosterageRequest.getPageNum(),
                getMyFosterageRequest.getPageSize()
        );
    }

    /**
     * 获取寄养信息详情接口
     * @param id 寄养数据id
     * @return
     */
    @Operation(summary = "获取寄养信息详情接口")
    @Parameter(name = "id", description = "寄养数据id")
    @GetMapping("/getBailmentDetail")
    public R getBailmentDetail(@RequestParam("id") Long id) {
        return bailmentDataService.getBailmentDetail(id);
    }

    /**
     * 删除寄养数据接口
     * @param id 寄养数据id
     * @param request 获取用户id
     * @return
     */
    @Operation(summary = "删除寄养数据接口")
    @Parameter(name = "id", description = "寄养数据id，拼接在url上")
    @DeleteMapping("/deleteBailmentData")
    public R deleteBailmentDetail(@RequestParam("id") Long id, HttpServletRequest request) {
        String token = request.getHeader("token");
        return bailmentDataService.deleteBailmentData(id, JwtUtil.getId(token));
    }

    /**
     * 搜索寄养数据接口
     * @param searchBailmentDataRequest 查询条件
     * @return
     */
    @Operation(summary = "搜索寄养数据接口")
    @PostMapping("/searchBailmentData")
    public R searchBailmentData(@RequestBody SearchBailmentDataRequest searchBailmentDataRequest) {
        log.debug("前端传来的数据：{}", searchBailmentDataRequest);
        SearchBailmentDataDTO searchBailmentDataDTO = BailmentDataConverter.INSTANCE.toGetBailmentDataDTO(searchBailmentDataRequest);
        log.debug("转换后的数据：{}", searchBailmentDataDTO);
        return bailmentDataService.searchBailmentData(searchBailmentDataDTO);
    }
}
